package com.shujia.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object Demo1KafkaProduce {
  def main(args: Array[String]): Unit = {

    /**
      *
      * 1、创建生产者
      */

    val properties = new Properties()

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //创建生产者
    val producer = new KafkaProducer[String, String](properties)


    /**
      * 如果topic不存在会自动创建，创建一个分区为1,副本为1的topic
      *
      */

    //生产数据

    val record = new ProducerRecord[String, String]("text", "java")

    //发送数据到kafka集群
    producer.send(record)

    //刷新
    producer.flush()


    //关闭链接
    producer.close()

  }
}
